/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ut_frstat_server_related_add.c                     */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frame_params.h"
#include "frstat_server.h"
#include "apmtest_cunit.h"

extern int g_i_flag_run;
extern unsigned long g_ul_pcap_queue_used;
extern Frapm_params st_apm_params;

typedef struct __conn_info {
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
} conn_info;

typedef struct __conn_avg {
    unsigned long ul_connections[4];
    unsigned long ul_requests[4];
    unsigned long ul_responses[4];
} conn_avg;

typedef struct __server_info {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    struct timeval start_time;
    conn_info st_conns_total;
    conn_avg  st_conns_avg;
} Server_info;

//端口关联表
typedef struct __port_related {
    u_int   ui_ip;
    u_short us_port;
    u_int   ui_ip_related;
    u_short us_port_related;
    
    struct timeval start_time;
} Port_related;

//做完关联后的hash节点
typedef struct __server_info_avg {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
    
    unsigned long ul_connections_avg;
    unsigned long ul_requests_avg;
    unsigned long ul_responses_avg;
} Server_info_avg; 

extern Frhash_header g_st_hash_related;

static int __suite_init(void)
{
    g_i_flag_run = 1;
    g_ul_pcap_queue_used = 0L;
    st_apm_params.ui_stat_server_hash_size = 10;
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

void __frstat_related_show(Frlist_node* p_node, char *p_outbuff, unsigned int ui_outlen)
{
    Port_related *p_stat_node = NULL;
    
    p_stat_node = (Port_related *)p_node->data;
    
    snprintf(p_outbuff, ui_outlen
           , "ip=%u, port=%u, ip_related=%u, port_related=%u\n"
           , p_stat_node->ui_ip
           , p_stat_node->us_port
           , p_stat_node->ui_ip_related
           , p_stat_node->us_port_related);
    
    return;
}

static void case1(void)
{
    u_int ui_ip = 16781484;
    u_short us_port = 80;
    u_int ui_ip_related = 83890348;
    u_short us_port_related = 8000;
    int i_ret = 0;
    char *p_outbuff = NULL;
    
    frstat_server_init(1);
    
    i_ret = frstat_server_related_add(ui_ip, us_port, ui_ip_related, us_port_related);

    frhash2_show_all2(&g_st_hash_related, &p_outbuff, __frstat_related_show);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("related=[%s]", p_outbuff);
    
    frstat_server_destory();
    
    frfree(p_outbuff);

    return;

}

static void case2(void)
{
    u_int ui_ip = 16781484;
    u_short us_port = 80;
    u_int ui_ip_related = 83890348;
    u_short us_port_related = 9000;
    int i_ret = 0;
    char *p_outbuff = NULL;
    
    frstat_server_init(1);
    
    frstat_server_related_add(16781484, 80, 83890348, 8000);
    
    i_ret = frstat_server_related_add(ui_ip, us_port, ui_ip_related, us_port_related);

    frhash2_show_all2(&g_st_hash_related, &p_outbuff, __frstat_related_show);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("related=[%s]", p_outbuff);
    
    frstat_server_destory();
    
    frfree(p_outbuff);

    return;

}

static void case3(void)
{
    u_int ui_ip = 16781484;
    u_short us_port = 8080;
    u_int ui_ip_related = 83890348;
    u_short us_port_related = 9000;
    int i_ret = 0;
    char *p_outbuff = NULL;
    
    frstat_server_init(1);
    
    frstat_server_related_add(16781484, 80, 83890348, 8000);
    
    i_ret = frstat_server_related_add(ui_ip, us_port, ui_ip_related, us_port_related);

    frhash2_show_all2(&g_st_hash_related, &p_outbuff, __frstat_related_show);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("related=[%s]", p_outbuff);
    
    frstat_server_destory();
    
    frfree(p_outbuff);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("ut_frstat_server_related_add 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}